<?php
/*
 * @开发工具: JetBrains PhpStorm.
 * @文件名：loan.class.php
 * @类功能: 贷款产品
 * @开发者: 陈旭林
 * @开发时间： 2017-09-01
 * @版本：version 1.0
 */

namespace app\api\controller;


use app\common\cache\agent\IAgentAdminCache;
use app\common\cache\agent\IAgentAdminFavoriteCache;
use app\common\cache\agent\IAgentAdminRecommendCache;
use app\common\cache\agent\IAgentCache;
use app\common\controller\ApiBaseController;
use app\common\enum\AgentAdminStatus;
use app\common\enum\IsCompany;
use app\common\enum\IsDelete;
use app\common\enum\IsAnonymous;
use app\common\enum\IsAuthentication;
use app\common\enum\IsPhone;
use app\common\enum\Sex;
use app\common\ext\IDb;

class AgentAdmin extends ApiBaseController {
    /**
     * @功能：取得推荐顾问
     * @开发者：gys
     */
    public function recommend(){
        //接收参数
        $province_id  = self::get_data('province_id',self::NOT_EMPTY,"省份编号不能为空！");
        $city_id  = self::get_data('city_id',self::NOT_EMPTY,"城市编号不能为空！");
        $recommend_location = self::get_data('recommend_location',self::NOT_EMPTY,"推荐位置不能为空！");

        //判断列表是否存在
        $recommend_list = IAgentAdminRecommendCache::getList($recommend_location,$province_id,$city_id);
        if($recommend_list === false){
            self::set_code(self::ERR_0001);
            self::set_msg("取得顾问列表失败！");
            self::send();
        }

        //判断列表是否存在
        if(empty($recommend_list)){
            $recommend_list = array();
        }

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("recommend_list",$recommend_list);
        self::send();
    }

    /**
     * @功能：取得推荐顾问详情
     * @开发者：gys
     */
    public function agent_admin_detail(){
        //接收参数
        $agent_admin_id  = self::get_data('agent_admin_id',self::NOT_EMPTY,"顾问编号不能为空！");

        $agent_admin_info = IAgentAdminCache::getInfo($agent_admin_id,['agent_id','real','avatar','phone','evaluate_rate','business_phone','service_ote','is_company','is_real']);
        if(empty($agent_admin_info)){
            self::set_code(self::WARNING_0001);
            self::set_msg("取得顾问详情失败！");
            self::send();
        }

        if(!empty($agent_admin_info['phone'])){
            $agent_admin_info['is_phone'] = IsPhone::yes;
        }else{
            $agent_admin_info['is_phone'] = IsPhone::not;
        }

        if(empty($agent_admin_info['business_phone'])){
            $agent_admin_info['business_phone'] = $agent_admin_info['phone'];
        }
        unset($agent_admin_info['phone']);

        $agent_info = IAgentCache::getInfo($agent_admin_info['agent_id'],['agent_name','business_license_url','is_bond','bond']);

        $is_exist = IDb::getInstance('agent_admin_favorite')
            ->setDbWhere(['user_id'=>self::$uid,'agent_admin_id'=>$agent_admin_id,'is_delete'=>IsDelete::No])
            ->row();

        $agent_admin_info['is_favorite'] = $is_exist ? 2 : 1;

        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("agent_admin_info",$agent_admin_info);
        self::set_value("agent_info",$agent_info);
        self::send();

    }

    /**
     * @功能：取得顾问评价
     * @开发者：gys
     */
    public function evaluate_list(){
        //接收参数
        $agent_admin_id  = self::get_data('agent_admin_id',self::NOT_EMPTY,"顾问编号不能为空！");
        $bottom_id = self::get_data('bottom_id');

        if(!empty($bottom_id)) $where['concat(1,lpad(agent_admin_evaluate_id,8,0))'] = array("lt",$bottom_id);
        $where['aae.agent_admin_id'] = $agent_admin_id;
        $evaluate_list = IDb::getInstance('agent_admin_evaluate aae')
            ->setDbFiled('aae.agent_admin_evaluate_id,aae.content,aae.create_time,aae.is_anonymous,u.avatar,u.real,u.sex')
            ->setDbJoin("user as u","u.user_id=aae.user_id ")
            ->setDbWhere($where)
            ->setDbOrder("agent_admin_evaluate_id desc")
            ->setDbLimit(10)
            ->sel();

        foreach ($evaluate_list as &$val){
            if ($val['is_anonymous'] == IsAnonymous::Yes){
                $val['real'] = get_real_cover($val['real'],$val['sex']);
            }

            $val['create_time'] = date('Y.m.d H:i',strtotime($val['create_time']));

            //设置排序编码
            $val['code'] = '1'.str_pad($val['agent_admin_evaluate_id'],8,0,STR_PAD_LEFT);
        }

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("evaluate_list",$evaluate_list);
        self::send();
    }
    /**
     * @功能：顾问列表
     * @开发者：gys
     */
    public function agent_admin_list(){
        //接收参数
        $province_id  = self::get_data('province_id',self::NOT_EMPTY,"省份编号不能为空！");
        $city_id  = self::get_data('city_id',self::NOT_EMPTY,"城市编号不能为空！");
        $bottom_id  = self::get_data('bottom_id');

        //设置查询条件
        $where['ca.is_delete'] = IsDelete::No;
        $where['ca.agent_admin_status'] = AgentAdminStatus::normal;
        $where['ca.audit_state'] = IsAuthentication::Yes;

        //判断列表是否存在
        $agent_admin_list = IAgentAdminCache::getList($where,$province_id,$city_id,$bottom_id);
        if($agent_admin_list === false){
            self::set_code(self::WARNING_0001);
            self::set_msg("搜索顾问失败！");
            self::send();
        }

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("agent_admin_list",$agent_admin_list);
        self::send();
    }

    /**
     * @功能：设置顾问收藏
     * @开发者：cxl
     */
    public function agent_admin_favorite(){
        //接收参数
        $user_id  = self::$uid;
        $agent_admin_id  = self::get_data('agent_admin_id',self::NOT_EMPTY,"顾问编码不能为空！");

        //实例化对象
        $credit_product_info = IAgentAdminCache::getInfo($agent_admin_id);
        if(empty($credit_product_info['agent_admin_id'])){
            self::set_code(self::WARNING_0001);
            self::set_msg("顾问不存在！");
            self::send();
        }

        //判断是否已经收藏
        $agent_admin_favorite_where['user_id'] = $user_id;
        $agent_admin_favorite_where['agent_admin_id'] = $agent_admin_id;
        $agent_admin_favorite_info = IDb::getInstance("agent_admin_favorite")->setDbFiled("agent_admin_favorite_id")->setDbWhere($agent_admin_favorite_where)->row();
        if(empty($agent_admin_favorite_info['agent_admin_favorite_id'])) {
            //添加收藏信息
            $agent_admin_favorite_data['user_id'] = $user_id;
            $agent_admin_favorite_data['agent_admin_id'] = $agent_admin_id;
            $agent_admin_favorite_add = IDb::getInstance("agent_admin_favorite")->setDbData($agent_admin_favorite_data)->add();
            if($agent_admin_favorite_add === false) {
                self::set_code(self::WARNING_0002);
                self::set_msg("收藏失败！");
                self::send();
            }
        }else{
            //添加收藏信息
            $agent_admin_favorite_data['is_delete'] = IsDelete::No;
            $agent_admin_favorite_upd = IDb::getInstance("agent_admin_favorite")->setDbData($agent_admin_favorite_data)->setDbWhere($agent_admin_favorite_where)->upd();
            if($agent_admin_favorite_upd === false) {
                self::set_code(self::WARNING_0002);
                self::set_msg("收藏失败！");
                self::send();
            }

            //收藏编号
            $agent_admin_favorite_add = $agent_admin_favorite_info['agent_admin_favorite_id'];
        }

        //清除缓存
        IAgentAdminFavoriteCache::deleteList($user_id);

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("agent_admin_favorite_id",intval($agent_admin_favorite_add));
        self::send();
    }

    /**
     * @功能：取消顾问收藏
     * @开发者：cxl
     */
    public function agent_admin_favorite_cancel(){
        //接收参数
        $user_id  = self::$uid;
        $credit_id  = self::get_data('agent_admin_id',self::NOT_EMPTY,"顾问编码不能为空！");

        //实例化对象
        $credit_product_info = IAgentAdminCache::getInfo($credit_id);
        if(empty($credit_product_info['agent_admin_id'])){
            self::set_code(self::WARNING_0001);
            self::set_msg("顾问不存在！");
            self::send();
        }

        //添加收藏信息
        $agent_admin_favorite_where['user_id'] = $user_id;
        $agent_admin_favorite_where['agent_admin_id'] = $credit_id;
        $agent_admin_favorite_data['is_delete'] = IsDelete::Yes;
        $agent_admin_favorite_upd = IDb::getInstance("agent_admin_favorite")->setDbData($agent_admin_favorite_data)->setDbWhere($agent_admin_favorite_where)->upd();
        if($agent_admin_favorite_upd === false) {
            self::set_code(self::WARNING_0002);
            self::set_msg("收藏失败！");
            self::send();
        }

        //清除缓存
        IAgentAdminFavoriteCache::deleteList($user_id);

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：用户收藏顾问列表
     * @开发者：cxl
     */
    public function user_agent_admin_favorite(){
        //接收参数
        $user_id  = self::$uid;
        $bottom_id  = self::get_data('bottom_id');

        //判断列表是否存在
        $favorite_list = IAgentAdminFavoriteCache::getList($user_id,$bottom_id);
        if (empty($favorite_list) || !is_array($favorite_list)) {
            $favorite_list = array();
        }

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("favorite_list",$favorite_list);
        self::send();
    }
}
